#import "ImagePreview.h"
#import "UIImageView+WebCache.h"

@interface ImagePreview()
@property (strong,nonatomic) UIScrollView *scrollView;
@property (nonatomic) CGFloat touchX;
@property (nonatomic) CGFloat touchY;
@property (nonatomic) BOOL isTwiceTaping;
@property (nonatomic) CGFloat currentScale;
@end

@implementation ImagePreview


- (id)initWithFrame:(CGRect)frame Url:(NSString *)url
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;

        self.scrollView.delegate = self;
        self.scrollView.minimumZoomScale = 1;
        self.scrollView.maximumZoomScale = 3;
        self.scrollView.backgroundColor = [UIColor clearColor];
        
        self.imgView = [[UIImageView alloc] init];
        
        self.imgView.contentMode = UIViewContentModeScaleToFill;
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"building_detail_layout_default.png"]];
        self.imgView.size = CGSizeMake(320, 200);
        self.imgView.center = self.scrollView.center;
//        imgView.frame = CGRectAddY(imgView.frame, -20);
        self.imgView.tag = 22;
        [self.scrollView addSubview:self.imgView];
        [self addSubview:self.scrollView];
        
        self.tapImgViewTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgViewHandleTwice:)];
        self.tapImgViewTwice.numberOfTapsRequired = 2;
        self.tapImgViewTwice.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:self.tapImgViewTwice];
    }
    return self;
}

- (void)returnScale {
    if (self.scrollView) {
        [self.scrollView setZoomScale:1.0 animated:YES];

    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    UIView *subView = [scrollView viewWithTag:22];
    return subView;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
    CGFloat xcenter = scrollView.center.x;
    CGFloat ycenter = scrollView.center.y;
    xcenter = scrollView.contentSize.width > scrollView.frame.size.width?scrollView.contentSize.width/2 :xcenter;
    ycenter = scrollView.contentSize.height > scrollView.frame.size.height ?scrollView.contentSize.height/2 : ycenter;
    UIView *subView = [scrollView viewWithTag:22];
    subView.center = CGPointMake(xcenter, ycenter);
//    subView.frame = CGRectAddY(subView.frame, -20);
    scrollView.contentSize = CGSizeMake(subView.width, subView.height);
}

-(void)tapImgViewHandleTwice:(UIGestureRecognizer *)sender{
    self.touchX = [sender locationInView:sender.view].x;
    self.touchY = [sender locationInView:sender.view].y;
    if(self.isTwiceTaping){
        return;
    }
    self.isTwiceTaping = YES;
    
    if(self.currentScale > 1.0){
        self.currentScale = 1.0;
        [self.scrollView setZoomScale:1.0 animated:YES];
    }else{
        self.currentScale = 3.0;
        [self.scrollView setZoomScale:3.0 animated:YES];
    }
    //延时做标记判断，使用户点击3次时的单击效果不生效。
    [self performSelector:@selector(twiceTaping) withObject:nil afterDelay:0.65];
}

-(void)twiceTaping{
    self.isTwiceTaping = NO;
}

@end
